package com.telchina.getui.util;

import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushBatchDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.telchina.getui.dto.PushMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Auther: zlm
 * @Date: 2023/5/6
 * @Description: com.telchina.getuitest.getui
 * @Version: 1.0
 */
@Slf4j
public class PushUtil implements ApplicationContextAware {

    private static PushApi pushApi;

    /**
     * 根据cid单推送
     *
     * @param message
     */
    public static void pushSingleByCid(PushMessageDTO message) {
        if (Objects.isNull(pushApi)) return;
        List<String> cids = message.getCids();
        if (Objects.isNull(cids) || cids.isEmpty()) return;
        if (cids.size() > 1) {
            log.warn("cid单推只会给第一个cid用户发送推送，其余cid用户将无法收到推送");
        }
        PushDTO<Audience> pushDTO = buildPushDTO(message, cids.get(0));
        // 进行cid单推
        StopWatch watch = new StopWatch();
        watch.start();
        log.info("cid单推——开始，内容:{}", message.getBody());
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        watch.stop();
        log.info("cid单推——结束，耗时：{}", watch.getTotalTimeMillis());
        if (apiResult.isSuccess()) {
            log.info("cid单推成功：{}", apiResult);
        } else {
            log.info("cid单推失败：{}", apiResult);
        }
    }

    /**
     * 根据cid批量推送
     *
     * @param message
     */
    public static void pushBatchByCid(PushMessageDTO message) {
        if (Objects.isNull(pushApi)) return;
        List<String> cids = message.getCids();
        if (Objects.isNull(cids) || cids.isEmpty()) return;
        List<PushDTO<Audience>> msgList = new ArrayList<>(cids.size());
        for (String cid : cids) {
            msgList.add(buildPushDTO(message, cid));
        }
        PushBatchDTO pushBatchDTO = new PushBatchDTO();
        pushBatchDTO.setAsync(Boolean.FALSE);
        pushBatchDTO.setMsgList(msgList);

        StopWatch watch = new StopWatch();
        watch.start();
        log.info("cid批量推送——开始，内容:{}", message.getBody());
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushBatchByCid(pushBatchDTO);
        watch.stop();
        log.info("cid批量推送——结束，耗时：{}", watch.getTotalTimeMillis());
        if (apiResult.isSuccess()) {
            log.info("cid批量推送成功：{}", apiResult);
        } else {
            log.info("cid批量推送失败：{}", apiResult);
        }
    }

    private static PushDTO<Audience> buildPushDTO(PushMessageDTO message, String cid) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        // 通知消息内容
        GTNotification notification = new GTNotification();
        notification.setTitle(message.getTitle());
        notification.setBody(message.getBody());
        notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_PAYLOAD.type);
        notification.setPayload(message.getExt());
        notification.setChannelLevel(CommonEnum.ChannelLevelEnum.LEVEL_FOUR.level + "");
        notification.setBadgeAddNum("1");
        pushMessage.setNotification(notification);
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/
        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);
        /*设置接收人信息结束*/
        return pushDTO;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        pushApi = applicationContext.getBean(PushApi.class);
    }
}
